import { HTTP_REQUEST_URL, HEADER, TOKENNAME } from "@/config/app";
import store from "../store";
import util from "./util";
import { checkTokenEndTime, authTokenClear } from "./auth.js";
import RequestUrl from "@/js_sdk/luch-request/request";
import { whiteList, noNeedTokenApis } from "./whiteApi.js";
import { editSign } from "./reqestSign.js";
const request = new RequestUrl();
// let whiteList = ["/tax/share/getShareReport"];
request.setConfig((config) => {
  config.baseUrl = HTTP_REQUEST_URL;
  config.header = HEADER;
  return config;
});

// 请求拦截
request.interceptor.request(
  (config) => {
    let signObj = {};
    if (config.method == "GET" || config.method == "PUT") {
      signObj = editSign(config.params);
    } else if (config.method == "POST" || config.method == "DELETE") {
      signObj = editSign(config.data);
    }
    if (whiteList.find((v) => config.url.split("?")[0].includes(v))) {
      if (
        store.state.app.token &&
        store.state.app.token != "" &&
        store.state.app.token != "undefined" &&
        !noNeedTokenApis.includes(config.url)
      ) {
        config.header[TOKENNAME] = store.state.app.token;
      }
      config.header = {
        ...config.header,
        ...signObj,
      };

      return config;
    } else {
      if (store.state.app.tokenStr && !checkTokenEndTime()) {
        // #ifdef H5 ||APP-PLUS
        if (
          store.state.app.token &&
          store.state.app.token != "" &&
          store.state.app.token != "undefined"
        ) {
          config.header[TOKENNAME] = store.state.app.token;
        } else {
          config.header = {
            "content-type": "application/json",
          };
        }
        // #endif


        // #ifdef MP
        console.log('store.state?.app?.token_wx', store.state?.app?.token_wx)
        if (store.state?.app?.token_wx) {
          console.log("token_wx", store.state.app.token_wx);
          config.header[TOKENNAME] = store.state.app.token_wx;
        } else {
          console.log("token_wx", "1111");
          config.header = {
            "content-type": "application/json",
          };
        }
        // #endif

        config.header = {
          ...config.header,
          ...signObj,
        };
        return config;
      } else {
        // #ifdef H5 || APP-PLUS
        // console.log(config,'请求拦截里面的config配置')
        authTokenClear();
        // #endif
        // #ifdef MP
        store.commit("LOGOUT");
        util.Tips({ title: "请重新登录" }, () => {
          uni.navigateTo({
            url: "/pagesWater/users/login",
          });
        });
        // #endif
      }
    }
  },
  (error) => {
    Promise.reject(error);
  }
);

// 响应拦截
request.interceptor.response(
  async (cb) => {
    if (cb.data.code === 200) return cb.data;
    else if (cb.data.code === 201) {
      setTimeout(() => {
        util.Tips(cb.data.message || "接口错误！");
      }, 1000);
    } else if (cb.data.code === 500) {
      setTimeout(() => {
        util.Tips({
          title: cb.data.message || "系统错误，请联系管理员！",
          endtime: 3000,
        });
      }, 1000);
      // #ifdef MP
      // location.reload()
      // #endif
      return Promise.reject(cb.data.message || "系统错误");
    } else if (cb.data.code === 403) {
      setTimeout(() => {
        util.Tips(cb.data.message || "系统错误");
      }, 1000);
      return cb.data;
    } else if ([410000, 410001, 410002, 401].indexOf(cb.data.code) !== -1) {
      store.commit("LOGOUT");
      // #ifdef H5 || APP-PLUS || MP-WEIXIN
      store.commit("LOGOUT");
      setTimeout(() => {
        util.Tips("请重新登录！");
      }, 1000);

      setTimeout(() => {
        uni.navigateTo({
          url: "/pagesWater/users/login",
        });
      }, 1500);

      return Promise.reject(cb.data);
      // #endif
    } else
      setTimeout(() => {
        util.Tips(cb.data.message || "系统错误");
      }, 1000);
    return Promise.reject(cb.data.message || "系统错误");
  },
  (ecb) => {
    if (ecb.data.code === 401) {
      authTokenClear();
    }
    return Promise.reject("请求失败");
  }
);

export default request;
